package com.readpost.service.impl;


import com.readpost.doman.Entity.ArticleCategories;
import com.readpost.mapper.ArticleCategoriesMapper;
import com.readpost.service.ArticleCategoriesService;
import com.readpost.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ArticleCategoriesServiceImpl implements ArticleCategoriesService {


    @Autowired
    private ArticleCategoriesMapper articleCategoriesMapper;

    /**
     * 增加文章分类
     *
     * @param category 文章分类信息
     */
    @Override
    public void addCategory(ArticleCategories category) {

//        Map<String,Object> map = ThreadLocalUtil.get();
//        String username = (String) map.get("username");
//        category.setModifiedUser(username);
        articleCategoriesMapper.addCategory(category);
    }

    /**
     * 判断分类名称是否存在
     *
     * @param categoryName 文章分类名称
     * @return  是否存在
     */
    @Override
    public boolean isCategoryNameExists(ArticleCategories categoryName) {
        int count = articleCategoriesMapper.countCategoryByName(categoryName);
        return count > 0;
    }

    /**
     * 判断分类ID是否存在
     *
     * @param categoryId 文章分类ID
     * @return 是否存在
     */
    @Override
    public boolean isCategoryIdExists(Integer categoryId) {
        int count = articleCategoriesMapper.countCategoryById(categoryId);
        return count > 0;
    }

    /**
     * 获取所有文章分类
     *
     * @return
     */
    @Override
    public List<ArticleCategories> getAllCategory() {
        return articleCategoriesMapper.getAllCategory();
    }


    /**
     * 根据分类Id查询
     *
     * @param categoryId 文章分类ID
     * @return 文章分类信息
     */
    @Override
    public ArticleCategories getCategoryById(Integer categoryId) {
        return articleCategoriesMapper.getCategoryById(categoryId);
    }

    /**
     * 删除文章分类
     * @param categoryId 文章分类ID
     */
    @Override
    public void deleteCategory(Integer categoryId){
        articleCategoriesMapper.deleteCategoryById(categoryId);
    }

    /**
     * 文章分类详情
     * @param  categoryId  文章分类ID
     */
    @Override
    public ArticleCategories getDetailCategory(Integer categoryId) {
        return articleCategoriesMapper.getCategoryById(categoryId);
    }

    /**
     * 修改文章分类
     * @param category 文章分类信息
     */
    @Override
    public void updateCategory(ArticleCategories category) {
        articleCategoriesMapper.updateCategory(category);
    }

}
